package com.ebt.m.users;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.ebt.m.R;
import com.ebt.m.commons.buscomponent.listview.IView;
import com.ebt.m.commons.utils.Logger;
import com.ebt.m.share.ShareDialog;
import com.ebt.m.users.bean.CardData;
import com.ebt.m.utils.ConfigData;
import com.ebt.m.utils.TalkingDataUtil;
import com.ebt.m.view.CircleImageView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CardNormalInfoView extends IView {


    @BindView(R.id.card_action_share)
    ImageButton cardActionShare;
    @BindView(R.id.card_action_edit)
    ImageButton cardActionEdit;
    @BindView(R.id.card_avatar)
    CircleImageView cardAvatar;
    @BindView(R.id.card_name)
    TextView cardName;
    @BindView(R.id.card_post)
    TextView cardPost;
    @BindView(R.id.card_phone)
    TextView cardPhone;
    @BindView(R.id.ll)
    LinearLayout ll;
    @BindView(R.id.card_company)
    TextView cardCompany;
    @BindView(R.id.item_root)
    FrameLayout itemRoot;
    CardData cardData;

    public CardNormalInfoView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflate(context, R.layout.layout_carddata_normal, this);
        ButterKnife.bind(this);
    }

    public CardNormalInfoView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CardNormalInfoView(Context context) {
        this(context, null);
    }

    @Override
    public void update(Object... obj) {
        try {
            cardData = (CardData) obj[0];
            cardName.setText(cardData.getUserName());
            cardPost.setText(cardData.getPost());
            cardCompany.setText(cardData.getCompanyName());
            cardPhone.setText(cardData.getPhone());
            Glide.with(getContext())
                    .load(cardData.getPortraitAddress())
                    .error(R.drawable.empty_photo)
                    .dontAnimate()
                    .into(cardAvatar);
        } catch (Exception e) {
            Logger.e(e);
        }

    }


    @OnClick({R.id.card_action_share, R.id.card_action_edit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.card_action_share:
                showShareDialog();
                break;
            case R.id.card_action_edit:
                gotoEdit();
                break;
        }
    }

    private void gotoEdit() {
        TalkingDataUtil.onEvent(TalkingDataUtil.TYPE_EDIT_CARD);
        Intent intent = new Intent(getContext(), CardEditActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("carddata", cardData);
        intent.putExtras(bundle);
        getContext().startActivity(intent);
    }

    private void showShareDialog() {
        if (cardData == null || TextUtils.isEmpty(cardData.getUserName())) {
            Toast.makeText(getContext(), "名片数据缺失", Toast.LENGTH_SHORT).show();
            return;
        }
        String shareUrl = cardData.getShareUrl();
        if (shareUrl != null && shareUrl.length() > 0) {
            List<String> hideList = new ArrayList<>();
            hideList.add(ShareDialog.COPY_LINK);
            new ShareDialog.Builder(getContext())
                    .setShareTitle(cardData.getUserName() + "的个人名片")
                    .setShareContent("您好，我是保险顾问" + cardData.getUserName() + "，愿竭诚为您服务！")
                    .setShareUrl(shareUrl)
                    .setHidePlatformList(hideList)
                    .setShareImagePath(ConfigData.PORTRAITPATH + File.separator + "card.png")
                    .setShareListener(null)
                    .setDialogDismissIfShareSuccess()
                    .create()
                    .show();
        }
        TalkingDataUtil.onEvent(TalkingDataUtil.TYPE_SHARE_CARD);
    }

}
